package com.ws.ucenterservice.controller;


import com.ws.commonutils.JwtUtils;
import com.ws.commonutils.R;
import com.ws.ucenterservice.entity.UcenterMember;
import com.ws.ucenterservice.entity.vo.LoginUser;
import com.ws.ucenterservice.entity.vo.RegisterUser;
import com.ws.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/eduucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @PostMapping("login")
    public R login(@RequestBody LoginUser loginUser) {
       String token= memberService.login(loginUser);
       return R.ok().data("token",token);
    }

    @PostMapping("register")
    public R register(@RequestBody RegisterUser registerUser) {

        Boolean res=memberService.register(registerUser);
        if(!res){
            return R.error().message("注册失败");
        }
        return R.ok();
    }

    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(id);
        return R.ok().data("member",member);

    }

    @GetMapping("getOrderMember/{id}")
    public com.ws.commonutils.UcenterMember getOrderMember(@PathVariable("id") String id) {
        UcenterMember member = memberService.getById(id);
        com.ws.commonutils.UcenterMember ucenterMember = new com.ws.commonutils.UcenterMember();
        BeanUtils.copyProperties(member,ucenterMember);
        return ucenterMember;
    }

    @GetMapping("getCountRegister/{day}")
    public R getCountRegister(@PathVariable("day") String day) {
        Integer cnt=memberService.getCountRegister(day);
        return R.ok().data("cnt",cnt);
    }

}

